// locutor command line client
// (c) 2009, Normen Müller, normen.mueller@gmail.com  
// $Id: Locutor.scala 86 2010-05-11 21:32:13Z normen.mueller $
package locutor.demo

import java.io.File

import scaup.Views._

import scalaxx.diff._

import locutor.Views._
import locutor.rws.RewriteSystem
import locutor.rws.rs.RuleSystem

/** A Change Impact Analysis workflow demonstation powered by locutor.
 *   
 *  @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.4
 * 
 *  @note due to GrGen's export functionality only "-->" edges are supported
 *  @todo ad hoc document type specific configurations, i.e. mapping of MIME/ DTD/ Document to a configuration comprising an equivalence specification, a graph model,
 *    and a rule system.  User annotations extending the computed dependency graph are optional.
 *  @todo introduce a ``GrGen status'', i.e. don't re-generate the rewrite system DLLs in each turn around but rather re-use those upto modifications; the major delay
 *    for a change impact analysis is in the GrGen rewrite system generation 
 */
object Locutor {  
  def main(args: Array[String]) {   
    // === Initialize system environment
    initialize
    
    // === Document Collection
    // A collection of documents C is created whereas each document Doc must have an associated document model M.
    // For each document D within the document collection C, the associated graph model G is extended by the
    // structural node types N_syn and structural link types L_syn of D, such that M(Doc) = <E, G join (N_syn, L_syn), R>.
    val C = DocumentCollection(
      "wedding",
      Some("wedding/wedding.rs"),
      ("etc/data/wedding/guests.xml",  DocumentModel("wedding/guests.eq", "wedding/guests.rs")),
      ("etc/data/wedding/seating.xml", DocumentModel("wedding/seating.eq"))
    )
    
    // === Simulate a change
    // v1: modification: list of invited guests permuted | deltas: empty | effects: none
    //"etc/data/wedding/guestsv1.xml".toFile copyTo "etc/data/wedding/guests.xml".toFile
    // v2: modification: a new invited guests | deltas: append | effects: seatings
    //"etc/data/wedding/guestsv2.xml".toFile copyTo "etc/data/wedding/guests.xml".toFile
    // v3: modification: a guest canceled | deltas: update | effects: seat
    "etc/data/wedding/guestsv3.xml".toFile copyTo "etc/data/wedding/guests.xml".toFile
    
    // === Semantic difference analysis
    val Δ = C.analyze
    
    // === Semantic dependency analysis
    // The dependency graph on the collection is computed comprising structural as well as semanitc nodes and links.
    implicit val D = C.annotate
    
    // === Change impact analysis
    val I = C ↩ Δ
    
    // === Revert simulated change
    "etc/data/wedding/guestsv0.xml".toFile copyTo "etc/data/wedding/guests.xml".toFile    
  }
  
  /** System configuration
   * 
   *  @todo externalize to XML configuration file
   */
  def initialize = {
    System.setProperty("locutor.tmp",   "/Users/nmueller/tmp/locutor")  // temporary directory
    System.setProperty("locutor.grgen", "/opt/grgen/bin")               // GrGen home directory
    System.setProperty("locutor.kb",    "etc/kb")                       // Knowledge base
  }  
}
